﻿using ConfExpert.Forms.Base;

using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Cikis
{
    public partial class CikisEmirleriSec : XForm
    {
        public CikisEmirleriSec()
        {
            InitializeComponent();
        }
        
        public int AtelyeId { get; set; }
        BindingSource bsCE;
        BindingSource bsCELines;
        private void CikisEmirleriSec_Load(object sender, EventArgs e)
        {
            
            bsCE = gvCikisEmri.Init("GetCikisEmriSec");
            bsCELines = gvCikisEmri.AddDetailGrid(gvCELines, "CikisEmriId");

            gvCELines.MakeColumnInvisible("CikisEmriId");

            gvCELines.AddSummary("Qty");
           
            GridManager gm = GridManager.GetManager(gvCikisEmri);
            gm.GridMenu.AddMenu("Seç", Sec);
            gm.GridMenu.AddMenu("Bırak", Birak);

            gvCikisEmri.MakeReadOnly();
            gvCELines.MakeReadOnly();

            this.Text = "Çıkış Emirleri";
            
            btnSec.ItemClick += btnSec_ItemClick;
            gvCikisEmri.RowStyle += gvCikisEmri_RowStyle;
            gvCikisEmri.DoubleClick += gvCikisEmri_DoubleClick;
            gvCikisEmri.FocusedRowChanged += gvCikisEmirleri_FocusedRowChanged;
            this.Shown += CikisEmirleriSec_Shown;

            gvCikisEmri.BestFitColumns();
            gvCELines.BestFitColumns();
        }

        void CikisEmirleriSec_Shown(object sender, EventArgs e)
        {
            splitContainerControl1.AutoSize(0.6);
        }

        void gvCikisEmri_DoubleClick(object sender, EventArgs e)
        {
            gvCikisEmri.SetFocusedRowCellValue("Seç", true);
            btnSec.PerformClick();
        }

        void gvCikisEmri_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if ((sender as GridView).GetRowCellValue(e.RowHandle, "Seç") != null)
                    if ((sender as GridView).GetRowCellValue(e.RowHandle, "Seç") != DBNull.Value)
                        if ((bool)(sender as GridView).GetRowCellValue(e.RowHandle, "Seç") == true)
                            e.Appearance.BackColor = Color.SkyBlue;
        }

        public string CEs = string.Empty;
        void btnSec_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvCikisEmri.EndEditing();
            gvCELines.EndEditing();
            int i = 0;
            bool differ = false, IsDestinationNull = false;
            object AtelyeId = null, CustomerId = null, VendorId = null;
            foreach (int rowHandle in gvCikisEmri.GetSelectedRows())
                if (rowHandle > -1)
                {
                    CEs += gvCikisEmri.GetRowCellValue(rowHandle, "CikisEmriId").ToString() + ",";

                    if (i++ < 1)
                    {
                        AtelyeId = gvCikisEmri.GetRowCellValue(rowHandle, "AtelyeId");
                        CustomerId = gvCikisEmri.GetRowCellValue(rowHandle, "CustomerId");
                        VendorId = gvCikisEmri.GetRowCellValue(rowHandle, "VendorId");
                    }
                    else
                        if (!gvCikisEmri.GetRowCellValue(rowHandle, "AtelyeId").Equals(AtelyeId) ||
                            !gvCikisEmri.GetRowCellValue(rowHandle, "CustomerId").Equals(CustomerId))
                        {
                            differ = true;
                            break;
                        }
                    if (AtelyeId.ToInt() == 0 && CustomerId.ToInt() == 0 && VendorId.ToInt() == 0)
                    {
                        IsDestinationNull = true;
                        break;
                    }
                }

            if (differ)
            {
                XtraMessageBox.Show("Farklı atölye ve/veya müşterilerin çıkışı bir arada olamaz!", "Warning", MessageBoxButtons.OK);
                CEs = string.Empty;
                return;
            }

            if (IsDestinationNull)
            {
                XtraMessageBox.Show("Atölye,müşteri ya da üretici bilgisini doldurun!", "Warning", MessageBoxButtons.OK);
                CEs = string.Empty;
                return;
            }

            CEs = CEs.RemoveIfEndsWith(",");

            this.AtelyeId = AtelyeId.ToInt();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }


        void Birak(object sender, EventArgs e)
        {
            foreach (int handle in gvCikisEmri.GetSelectedRows())
                gvCikisEmri.SetRowCellValue(handle, "Seç", false);
        }

        void Sec(object sender, EventArgs e)
        {
            foreach (int handle in gvCikisEmri.GetSelectedRows())
                gvCikisEmri.SetRowCellValue(handle, "Seç", true);
        }


        private void gvCikisEmirleri_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (bsCE != null && bsCE.Current != null)
            {
                txtInfo.Caption = "Çıkış Emri - " + ((DataRowView)bsCE.Current).Row["CikisEmriId"].ToString();
                gvCELines.BestFitColumns();
            }
        }
    }
}